package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.util.PageResult;
import com.itheima.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    //分页查询
    @Override
    public PageResult findPage(int page, int size) {
        //添加页码和每页条件
        Page<Brand> p = new Page<>(page, size);
        //查询每页信息
        LambdaQueryWrapper<Brand> wrapper =new LambdaQueryWrapper<>();
        Page<Brand> brandPage = brandMapper.selectPage(p, wrapper);
        //查询总条数
        Integer integer = brandMapper.selectCount(null);
        return new PageResult(integer,brandPage.getRecords());
    }
    //查询全部
    @Override
    public List<Brand> findAll() {
        List<Brand> list = brandMapper.selectList(null);
        return list;
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }
    @Override
    public void delete(String id) {
        brandMapper.deleteById(id);
    }

    @Override
    public Result findList(String search) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Brand::getName,search);
        List<Brand> brands = brandMapper.selectList(wrapper);
        return new Result(0,true,"成功",brands);
    }

        @Override
    public Result add(Brand brand) {
        brandMapper.insert(brand);
        return new Result(0,true,"成功",null);
    }

    @Override
    public Result findById(Integer brandId) {
        return new Result(0,true,"成功",brandMapper.selectById(brandId));
    }
}
